/**
 * (C)2013 Exlu Corporation. All rights reserved.
 * 文件名 : cn.exlu.wea.tpk.email.FileEmail.java
 * 注意事項   :
 */
package cn.exlu.coco.core.server.email;

import java.util.Date;

import javax.activation.DataHandler;
import javax.activation.DataSource;
import javax.activation.FileDataSource;
import javax.mail.BodyPart;
import javax.mail.Message;
import javax.mail.MessagingException;
import javax.mail.Multipart;
import javax.mail.Session;
import javax.mail.internet.AddressException;
import javax.mail.internet.InternetAddress;
import javax.mail.internet.MimeBodyPart;
import javax.mail.internet.MimeMessage;
import javax.mail.internet.MimeMultipart;

/**
 * @author Mark 
 * @version 1.0.1
 * 2013-2-1
 * FileEmail.java
 */
public class FileEmail extends Email {

    /* (non-Javadoc)
     * @see cn.exlu.wea.tpk.email.Email#createMessage(javax.mail.Session)
     */
    @Override
    Message createMessage(Session session) throws AddressException, MessagingException {
        MimeMessage message=new MimeMessage(session);
        message.setFrom(new InternetAddress(getSendFrom()));
        message.setRecipients(Message.RecipientType.TO, getSendTo());
        message.setSubject(getSubject());
        message.setSentDate(new Date());
        Multipart multipart=new MimeMultipart();
        BodyPart bodyPart=new MimeBodyPart();
        bodyPart.setText(getText());
        multipart.addBodyPart(bodyPart);
        bodyPart=new MimeBodyPart();
        DataSource dataSource=new FileDataSource(getFile().getPath());
        bodyPart.setDataHandler(new DataHandler(dataSource));
        bodyPart.setFileName(getFile().getName());
        multipart.addBodyPart(bodyPart);
        
        message.setContent(multipart);
        return message;

    }

}
